class Solution {
public:
    bool check(vector<int>& bloomDay, int m, int k, int limit) {
        int total = 0;
        int cur_cnt = 0;
        for(auto day : bloomDay) {
            if(day > limit)
                cur_cnt = 0;
            else
                cur_cnt++;
            if(cur_cnt >= k) {
                total++;
                cur_cnt = 0;
            }
        }
        return total >= m;
    }
    int minDays(vector<int>& bloomDay, int m, int k) {
        int n = bloomDay.size();
        if(m > n / k) return -1;

        int left = *min_element(bloomDay.begin(), bloomDay.end());
        int right = *max_element(bloomDay.begin(), bloomDay.end());
        while(left <= right) {
            int mid = (left + right) / 2;
            if(check(bloomDay, m, k, mid))
                right = mid-1;
            else
                left = mid+1;
        }
        return right + 1;
    }
};

